﻿using ProductSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductSystem.BLL
{
    public partial class Code_TypeBll
    {
        /// <summary>
        /// 获取码类型（CodeType）
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="curpage">当前页</param>
        /// <param name="pagesize">每页记录数</param>
        /// <param name="recordCount">总数</param>
        /// <returns></returns>
        public static List<Code_Type> GetList(string CodeType, int curpage, int pagesize, ref int recordCount)
        {
            List<Code_Type> list = new List<Code_Type>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                if (string.IsNullOrEmpty(CodeType))
                {
                    list = dbContext.Code_Types.Where(m => m.Status == 1).OrderByDescending(m => m.ID).Select(t => t).Skip((curpage - 1) * pagesize).Take(pagesize).ToList();
                    recordCount = dbContext.Code_Types.Where(m => m.Status == 1).Count();
                }
                else
                {
                    list = dbContext.Code_Types.Where(m => m.Status == 1 && (m.Type_Code.Contains(CodeType) || m.Type_Name.Contains(CodeType))).OrderByDescending(m => m.ID).Select(t => t).Skip((curpage - 1) * pagesize).Take(pagesize).ToList();
                    recordCount = dbContext.Code_Types.Where(m => m.Status == 1 && (m.Type_Code.Contains(CodeType) || m.Type_Name.Contains(CodeType))).Count();
                }
            }
            return list;
        }

        /// <summary>
        /// 获取所有码类型，不需要分页
        /// </summary>
        /// <param name="status"></param>
        /// <returns>list</returns>
        public static List<Code_Type> GetAllList(int status)
        {
            List<Code_Type> list = new List<Code_Type>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                list = dbContext.Code_Types.Where(m => m.Status == 1).ToList();
            }
            return list;
        }

        /// <summary>
        /// 是否存在该码类型
        /// </summary>
        /// <param name="Type_Code">类型号</param>
        /// <returns>true/false</returns>
        public static bool ExistTypeCode(int id, string Type_Code)
        {
            bool reStr = false;
            int total = 0;
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                total = dbContext.Code_Types.Where(m => m.Type_Code == Type_Code && m.ID == id).Count();
                reStr = total == 0 ? false : true;
            }
            return reStr;
        }

        /// <summary>
        /// 判断是否已存在该记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool IsExist(Code_Type model)
        {
            bool flag = false;
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                Code_Type item = dbContext.Code_Types.FirstOrDefault(o => o.Type_Code == model.Type_Code && o.ID != model.ID && o.Status != 0);
                if (item != null)
                {
                    flag = true;
                }
            }
            return flag;
        }

        /// <summary>
        /// 删除码类型及其相应的码
        /// </summary>
        /// <param name="ids">需要删除的码类型的ID集合</param>
        /// <returns></returns>
        public static bool DeleteTypeCode(string ids)
        {
            bool result = false;
            try
            {
                using (EntitiesModel dbContext = new EntitiesModel())
                {
                    string[] idList = ids.Split(',');
                    for (int i = 0; i < idList.Count(); i++)
                    {
                        Code_Type m = dbContext.Code_Types.FirstOrDefault(t => t.ID == Convert.ToInt32(idList[i]));
                        m.Status = 0;

                        foreach (Base_Code item in dbContext.Base_Codes.Where(t => t.Type_Code == m.Type_Code))
                        {
                            item.Status = 0;
                        }
                    }
                    dbContext.SaveChanges();
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            return result;
        }
    }
}
